using System;
using System.Web.Mvc;
using System.Web.Routing;
using Azure.Domain.Models;
using Azure.Web.Controllers;

namespace Azure.Web.Models
{
    public static class ModelExtensions
    {
        public static Uri Link(this UrlHelper url, Post post)
        {
            var routeData = new RouteValueDictionary
                                {
                                    {"action", Blog.View},
                                    {"controller", Blog.Controller},
                                    {"year", post.Year()},
                                    {"month", post.Month()},
                                    {"day", post.Day()},
                                    {"slug", post.Slug },
                                };

            var response = url.RouteUrl(routeData);
            return new Uri(url.Link(response));
        }

        public static string Link(this UrlHelper url, string relativePath)
        {
            var requestUrl = url.RequestContext.HttpContext.Request.Url;

            return String.Format("{0}://{1}{2}", requestUrl.Scheme, requestUrl.DnsSafeHost, relativePath);
        }

        public static Uri Link(this UrlHelper url, PostViewModel viewModel)
        {
            string response = url.Action(Blog.View, Blog.Controller,
                                       new
                                       {
                                           year = viewModel.Year,
                                           month = viewModel.Month,
                                           day = viewModel.Day,
                                           slug = viewModel.Slug
                                       });


            return new Uri(url.Link(response));
        }
    }

    public static class Data
    {
        public const string SiteName = "_site_name_";
        public const string Site = "_site_";
    }
}